package com.sunspring.zdcall.service.impl;

import com.sunspring.zdcall.common.exception.ZdException;
import com.sunspring.zdcall.entity.ZdConfigDatasource;
import com.sunspring.zdcall.mapper.master.DatasourceMapper;
import com.sunspring.zdcall.service.DatasourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wulei
 * @date 2018-12-26
 **/
@Service
@Slf4j
public class DatasourceServiceImpl implements DatasourceService {

    @Autowired
    private DatasourceMapper dsMapper;


    @Override
    public List<ZdConfigDatasource> getAllDatasource() {
        return dsMapper.selectAll();
    }

    @Override
    public void addDataSource(ZdConfigDatasource datasource) {
        int result = dsMapper.insertSelective(datasource);
        if (result <= 0) {
            throw new ZdException("新增数据源失败");
        }
        log.info("新增数据源:{}",datasource);
    }

    @Override
    public void updateDatasource(ZdConfigDatasource datasource) {
        int result = dsMapper.updateSelective(datasource);
        if (result <= 0) {
            throw new ZdException("更新数据源失败");
        }
    }

    @Override
    public void deleteDatasource(int sourceId) {
        int result = dsMapper.deleteById(sourceId);
        if (result <= 0) {
            throw new ZdException("删除数据源失败");
        }
        log.info("数据源：{}，已被删除",sourceId);
    }
}
